package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.LwkjMapper;
import com.hbnu.system.model.entity.Lwkj;
import com.hbnu.system.service.LwkjService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 论文科技一级学科(Lwkj)表服务实现类
 *
 * @author makejava
 * @since 2024-12-20 15:56:25
 */
@Service("lwkjService")
public class LwkjServiceImpl extends ServiceImpl<LwkjMapper, Lwkj> implements LwkjService {

    @Override
    public boolean addLwkj(Lwkj lwkj) {
        Lwkj projectcate1 = super.getOne(Wrappers.lambdaQuery(Lwkj.class)
                .eq(Lwkj::getName,lwkj.getName()));
        if(StrUtil.isEmpty(lwkj.getName())||StrUtil.isBlank(lwkj.getName())){
            throw new BaseException("该项目类别名称为空");
        }
        if(!Objects.isNull(projectcate1)){
            throw new BaseException("该学科分类名称已存在");
        }
        return baseMapper.insert(lwkj)==1;
    }

    @Override
    public List<Lwkj> getLwkjList() {
        List<Lwkj> depts = baseMapper.selectList(Wrappers.lambdaQuery(Lwkj.class));
//        List<String> result = new ArrayList<>();
//        for (Dept dept : depts) {
//            result.add(dept.getDeptName());
//        }
        return depts;
    }

    @Override
    public boolean updateLwkj(Lwkj lwkj) {
        if(StrUtil.isNotEmpty(lwkj.getName())){
            Lwkj projectcate1 = super.getOne(Wrappers.lambdaQuery(Lwkj.class)
                    .eq(Lwkj::getName,lwkj.getName()));
            if(!Objects.isNull(projectcate1)){
                throw new BaseException("该学科分类名称已存在");
            }
            return baseMapper.updateById(lwkj)==1;
        }
        return false;
    }

    @Override
    public boolean deleteLwkj(String ID) {
        return baseMapper.deleteById(ID) == 1;
    }

    @Override
    public List<Lwkj> getByLwkjName(String Name) {
        LambdaQueryWrapper<Lwkj> queryWrapper = Wrappers.lambdaQuery(Lwkj.class);

        // 如果 disciplineClassfiName 不为空且不是空白字符串，则添加模糊匹配条件
        if (StringUtils.isNotBlank(Name)) {
            queryWrapper.like(Lwkj::getName, Name);
        }

        return baseMapper.selectList(queryWrapper);
    }



}

